<template>
  <!--工具栏-->
  <div class="app-container">
    <div class="head-container">
      <crudOperation :permission="permission" />
    </div>
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="分成名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="分成描述">
          <el-input v-model="form.description" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="分成比例" prop="rate">
          <el-input-number v-model.number="form.rate" style="width: 370px;" max="40" min="0" />
        </el-form-item>
        <el-form-item label="下级人数" prop="number">
          <el-input-number v-model.number="form.number" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="状态" prop="enabled">
          <el-radio v-for="item in dict.dept_status" :key="item.id" v-model="form.enabled" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-row :gutter="15">
      <el-col :xs="24" :sm="24" :md="16" :lg="13" :xl="13" style="margin-bottom: 10px">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <span class="role-span">分成等级列表</span>
          </div>
          <el-table
            ref="table"
            v-loading="crud.loading"
            :data="crud.data"
            row-key="id"
            @select="crud.selectChange"
            @select-all="crud.selectAllChange"
            @selection-change="crud.selectionChangeHandler"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column label="分成名称" prop="name" />
            <el-table-column label="分成描述" prop="description" />
            <el-table-column label="分成比例" prop="rate">
              <template slot-scope="scope">
                <span>{{ scope.row.rate }}%</span>
              </template>
            </el-table-column>
            <el-table-column label="下级数" prop="number" />
            <el-table-column label="状态" align="center" prop="enabled">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.enabled"
                  active-color="#409EFF"
                  inactive-color="#F56C6C"
                  @change="changeEnabled(scope.row, scope.row.enabled,)"
                />
              </template>
            </el-table-column>
            <el-table-column
              v-if="checkPer(['admin','dept:edit','dept:del'])"
              label="操作"
              width="130px"
              align="center"
              fixed="right"
            >
              <template slot-scope="scope">
                <udOperation
                  :data="scope.row"
                  :permission="permission"
                  msg="确定删除吗，此操作不能撤销！"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="16" :lg="13" :xl="13">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <el-tooltip class="item" effect="dark" content="角色分成设置" placement="top">
              <span class="role-span">系统参数</span>
            </el-tooltip>
            <el-button
              v-permission="['admin','roles:edit']"
              icon="el-icon-check"
              size="mini"
              style="float: right; padding: 6px 9px"
              type="primary"
              @click="saveSystemParams"
            >保存</el-button>
          </div>
          <el-form ref="form" :model="systemForm" :rules="rules" style="margin-top: 15px;" size="small" label-width="130px">
            <el-card class="box-card" shadow="never">
              <div slot="header" class="clearfix">
                <span class="role-span">VIP会员价格</span>
              </div>
              <el-form-item label="原价" prop="price">
                <el-input v-model="systemForm.vipMemberPrice" style="width: 130px" />
                <span>元</span>
              </el-form-item>
              <el-form-item label="现价" prop="price">
                <el-input v-model="systemForm.vipMemberDiscountPrice" style="width: 130px" />
                <span>元</span>
              </el-form-item>
              <el-form-item label="VIP推荐晋升个数" prop="price">
                <el-input v-model="systemForm.vipMemberPromoteNumber" style="width: 130px" />
                <span>位</span>
              </el-form-item>
            </el-card>
            <el-card class="box-card" shadow="never" style="margin-top: 15px;">
              <div slot="header" class="clearfix">
                <span class="role-span">预付金</span>
              </div>
              <el-form-item label="咨询" prop="price">
                <el-input v-model="systemForm.prepaidQueryAmount" style="width: 130px" />
                <span>元</span>
              </el-form-item>
              <el-form-item label="合作" prop="price">
                <el-input v-model="systemForm.prepaidCooperationAmount" style="width: 130px" />
                <span>元</span>
              </el-form-item>
            </el-card>

            <el-card class="box-card" shadow="never" style="margin-top: 15px;">
              <div slot="header" class="clearfix">
                <span class="role-span">客服会员</span>
              </div>
              <el-form-item label="文章写作分成" prop="price">
                <el-input v-model="systemForm.articleAuthorIncomeRatio" style="width: 130px" />
                <span>%</span>
              </el-form-item>
              <el-form-item label="咨询回答分成" prop="price">
                <el-input v-model="systemForm.queryAuthorIncomeRatio" style="width: 130px" />
                <span>%</span>
              </el-form-item>
              <el-form-item label="合作回答分成" prop="price">
                <el-input v-model="systemForm.cooperationAuthorIncomeRatio" style="width: 130px" />
                <span>%</span>
              </el-form-item>
            </el-card>

            <el-card class="box-card" shadow="never" style="margin-top: 15px;">
              <div slot="header" class="clearfix">
                <span class="role-span">普通会员</span>
              </div>
              <el-card class="box-card" shadow="never">
                <div slot="header" class="clearfix">
                  <span class="role-span">购买折扣</span>
                </div>
                <el-form-item label="文章" prop="price">
                  <el-input v-model="systemForm.memberBuyArticleDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="咨询" prop="price">
                  <el-input v-model="systemForm.memberBuyQueryDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="合作" prop="price">
                  <el-input v-model="systemForm.memberBuyCooperationDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">分享文章分成</span>
                </div>
                <el-form-item label="分享者" prop="price">
                  <el-input v-model="systemForm.memberShareArticleIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="购买者上级" prop="price">
                  <el-input v-model="systemForm.memberArticleBuyParentIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">咨询分成</span>
                </div>
                <el-form-item label="购买者上级" prop="price">
                  <el-input v-model="systemForm.memberQueryBuyParentIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-form-item label="推荐VIP分成" prop="price" style="margin-top: 15px;">
                <el-input v-model="systemForm.memberVIPBuyParentIncomeRatio" style="width: 130px" />
                <span>%</span>
              </el-form-item>
            </el-card>

            <el-card class="box-card" shadow="never" style="margin-top: 15px;">
              <div slot="header" class="clearfix">
                <span class="role-span">VIP会员</span>
              </div>
              <el-card class="box-card" shadow="never">
                <div slot="header" class="clearfix">
                  <span class="role-span">购买折扣</span>
                </div>
                <el-form-item label="文章" prop="price">
                  <el-input v-model="systemForm.vipMemberBuyArticleDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="咨询" prop="price">
                  <el-input v-model="systemForm.vipMemberBuyQueryDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="合作" prop="price">
                  <el-input v-model="systemForm.vipMemberBuyCooperationDiscount" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">分享文章分成</span>
                </div>
                <el-form-item label="分享者" prop="price">
                  <el-input v-model="systemForm.vipMemberShareArticleIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
                <el-form-item label="购买者上级" prop="price">
                  <el-input v-model="systemForm.vipMemberArticleBuyParentIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">咨询分成</span>
                </div>
                <el-form-item label="购买者上级" prop="price">
                  <el-input v-model="systemForm.vipMemberQueryBuyParentIncomeRatio" style="width: 130px" />
                  <span>%</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">小额咨询</span>
                </div>
                <el-form-item label="价格" prop="price">
                  <el-input v-model="systemForm.vipMemberQuerySmallPrice" style="width: 130px" />
                  <span>元</span>
                </el-form-item>
                <el-form-item label="次数" prop="price">
                  <el-input v-model="systemForm.vipMemberQuerySmallCount" style="width: 130px" />
                  <span>次</span>
                </el-form-item>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 15px;">
                <div slot="header" class="clearfix">
                  <span class="role-span">到期提醒</span>
                </div>
                <el-form-item label="提前天数" prop="price">
                  <el-input v-model="systemForm.vipMemberAlertDayBefore" style="width: 130px" />
                  <span>天</span>
                </el-form-item>
              </el-card>
              <el-form-item label="推荐VIP分成" prop="price" style="margin-top: 15px;">
                <el-input v-model="systemForm.vipMemberVIPBuyParentIncomeRatio" style="width: 130px" />
                <span>%</span>
              </el-form-item>
              <el-form-item label="咨询免费次数" prop="price">
                <el-input v-model="systemForm.vipMemberQueryFreeCount" style="width: 130px" />
                <span>次</span>
              </el-form-item>
              <el-form-item label="追加咨询次数" prop="price">
                <el-input v-model="systemForm.vipMemberAddQueryNumber" style="width: 130px" />
                <span>次</span>
              </el-form-item>
            </el-card>

            <el-card class="box-card" shadow="never" style="margin-top: 15px;">
              <div slot="header" class="clearfix">
                <span class="role-span">APP相关</span>
              </div>
              <el-form-item label="苹果版本号" prop="price">
                <el-input v-model="systemForm.version" style="width: 130px" />
              </el-form-item>
              <el-form-item label="安卓版本号" prop="price">
                <el-input v-model="systemForm.androidVersion" style="width: 130px" />
              </el-form-item>
              <el-form-item label="强制更新" prop="price">
                <el-input v-model="systemForm.forceUpdate" style="width: 130px" />
                <span>0：否 1：是</span>
              </el-form-item>
              <el-form-item label="注册协议" prop="price">
                <el-input v-model="systemForm.registerProtocol" style="width: 230px" />
              </el-form-item>
              <el-form-item label="苹果审核注册协议" prop="price">
                <el-input v-model="systemForm.iOSRegisterProtocol" style="width: 230px" />
              </el-form-item>
              <el-form-item label="隐私协议" prop="price">
                <el-input v-model="systemForm.privacyProtocol" style="width: 230px" />
              </el-form-item>
              <el-form-item label="联系客服" prop="price">
                <el-input v-model="systemForm.contactService" style="width: 230px" />
              </el-form-item>
              <el-form-item label="问易得简介" prop="price">
                <el-input v-model="systemForm.briefIntro" style="width: 230px" />
              </el-form-item>
              <el-form-item label="APP顶部图片" prop="appBanner">
                <el-upload
                  class="avatar-uploader"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :action="fileUploadApi + '?name=appBanner'"
                >
                  <img v-if="systemForm.appBanner" :src="systemForm.appBanner" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </el-form-item>
              <el-form-item label="APP二维码图片" prop="qrCodeImage">
                <el-upload
                  class="avatar-uploader"
                  :show-file-list="false"
                  :on-success="handleQRCodeImageSuccess"
                  :before-upload="beforeAvatarUpload"
                  :action="fileUploadApi + '?name=qrCodeImage'"
                >
                  <img v-if="systemForm.qrCodeImage" :src="systemForm.qrCodeImage" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </el-form-item>
            </el-card>
          </el-form>
          <el-button
            v-permission="['admin','roles:edit']"
            icon="el-icon-check"
            size="mini"
            style="float: right; padding: 6px 9px"
            type="primary"
            @click="saveSystemParams"
          >保存</el-button>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudCommission from '@/api/system/commission'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, name: null, description: '1', enabled: 'true', number: 0, rate: 0 }
export default {
  name: 'Commission',
  components: { crudOperation, udOperation },
  cruds() {
    return CRUD({ title: '分成', url: 'api/commission', crudMethod: { ...crudCommission }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 设置数据字典
  dicts: ['dept_status'],
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi',
      'fileUploadApi'
    ])
  },
  data() {
    return {
      depts: [],
      systemForm: { priceRatio: 15, vipPriceRatio: 20, vipPrice: 200, vipChargePriceRatio: 15, articleRatio: 10, queryRatio: 10, freeCount: 5, articleDiscount: 90, dayBefore: 3, smallCount: 5, smallPrice: 5 },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '请输入下级人数', trigger: 'blur', type: 'number' }
        ],
        rate: [
          { required: true, message: '请输入分成比例', trigger: 'blur', type: 'number' }
        ]
      },
      permission: {
        add: ['admin', 'dept:add'],
        edit: ['admin', 'dept:edit'],
        del: ['admin', 'dept:del']
      },
      enabledTypeOptions: [
        { key: 'true', display_name: '正常' },
        { key: 'false', display_name: '禁用' }
      ]
    }
  },
  mounted() {
    crudCommission.getSystemParams().then(value => {
      this.systemForm = value
    })
  },
  methods: {
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      form.enabled = `${form.enabled}`
    },
    handleAvatarSuccess(res, file) {
      this.systemForm.appBanner = this.baseApi + '/file/' + res.type + '/' + res.realName
    },
    handleQRCodeImageSuccess(res, file) {
      this.systemForm.qrCodeImage = this.baseApi + '/file/' + res.type + '/' + res.realName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 20

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 20MB!')
      }
      return isJPG && isLt2M
    },
    saveSystemParams() {
      crudCommission.saveSystemParams(this.systemForm).then(value => {
        this.crud.notify('保存成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.dept_status[val] + '" ' + data.name + '分成规则, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudCommission.edit(data).then(res => {
          this.crud.notify(this.dict.label.dept_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(err => {
          data.enabled = !data.enabled
          console.log(err.response.data.message)
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control, ::v-deep .vue-treeselect__placeholder, ::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius:0;
  }
</style>
